package com.mandao.dbmysql.controller;

import com.mandao.dbmysql.entry.User;
import com.mandao.dbmysql.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description
 *      spring-jpa
 * @author woniu
 * @date 2020/6/18 11:28
 */
@RestController
public class MainController {

    @Autowired
    private UserRepository userRepository;

    /**
     * 添加新用户
     */
    @GetMapping(path = "/add")
    @ResponseBody
    public String addNewUser(@RequestParam String name, @RequestParam String email) {
        User u = new User();
        u.setName(name);
        u.setEmail(email);
        userRepository.save(u);
        return "Saved";
    }

    @GetMapping(value = "/all")
    public Iterable<User> getAllUsers() {
        return userRepository.findAll();
    }
}
